//
//  NKDiagnosisViewController.m
//  NewKY
//
//  Created by Dee on 2016/11/12.
//  Copyright © 2016年 Dee. All rights reserved.
//

#import "NKDiagnosisViewController.h"
#import "NKCaseAddOrmodifyVC.h"
#import "NKCaseSaveCell.h"
#import "NKCaseInform.h"
@interface NKDiagnosisViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NKCaseInform * caseInfor;

@property(nonatomic,weak) UIImageView * dataNilView;

@property(nonatomic,weak) UIImageView * TopView;

@property(nonatomic,weak) UIImageView* tempView;

@end

@implementation NKDiagnosisViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"病例管理";
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [self setupCaseView];
    
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    if (self.caseInforList == nil) {
    
        self.caseInforList = [[NSMutableArray alloc]init];
        

        
    }else{
    
       
    
    
    }
    


    [self loadPerson];

}
//读取沙河
- (void)loadPerson
{
    // -------- 获取保存路径 --------
    NSString *docDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *path = [docDir stringByAppendingPathComponent:@"Hero.hm"];
    
    // -------- 对归档文件进行解档操作 --------
//    NKCaseInform *hero = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

    NSMutableArray * arrtemp = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    if (arrtemp != nil) {
        _caseInforList = arrtemp;
        
        self.caseTableView.hidden = NO;
        self.dataNilView.hidden = YES;
        self.tempView.hidden = NO;
    }else{
    
        self.caseTableView.hidden = YES;
    
        self.dataNilView.hidden =NO;
        self.tempView.hidden = YES;
    }
   
    
    [self.caseTableView reloadData];
    
}





- (void)setupCaseView{
    
    
    
    //topView
    
    UIImageView * topView  = [[UIImageView alloc]init];
    
    topView.image = [UIImage imageNamed:@"Snip20161117_1"];
    topView.backgroundColor = [UIColor colorWithHexString:@"efeff4"];
    
    self.TopView = topView;
    
    [self.view addSubview:topView];
    
    
    UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    [tableView registerClass:[NKCaseSaveCell class] forCellReuseIdentifier:@"KAKA"] ;
    
    [self.view addSubview:tableView];
    
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    
    tableView.rowHeight = 160;
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-44);
        make.height.equalTo(@448);
    }];
    
    self.caseTableView = tableView;
    
    tableView.backgroundColor = [UIColor colorWithHexString:@"efeff4"];

    //底部的View
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn setTitle:@"  添加病例" forState:UIControlStateNormal];
    
    [btn setImage:[UIImage imageNamed:@"add"] forState:0];

    
    
//    btn.backgroundColor = [UIColor colorWithHexString:@"00C5C5"];
      btn.backgroundColor = [UIColor whiteColor];
    
    [btn setTitleColor:[UIColor colorWithHexString:@"00C5C5"] forState:0];
    
    [self.view addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(tableView.mas_bottom);
        
    }];
    
    [btn addTarget:self action:@selector(brnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //图片

    tableView.delegate = self;
    tableView.dataSource =self;

    

    
    //topView约束
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(tableView.mas_top).offset(-1.5);
        
    }];

    //没有数据的时候
    
    //没有Data的时候图片约束
    UIImageView * dataNilWiew = [[UIImageView alloc]init];
    
    dataNilWiew.contentMode = UIViewContentModeScaleAspectFit;
    
    dataNilWiew.image = [UIImage imageNamed:@"Snip20161118_13"];
    
    [dataNilWiew sizeToFit];
    
    [self.view addSubview:dataNilWiew];
    
    self.dataNilView = dataNilWiew;
    
    [dataNilWiew mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.left.right.equalTo(self.view);
        make.top.equalTo(topView.mas_bottom);
        make.bottom.equalTo(btn.mas_top);
        
    }];
    

    //分割线
    UIImageView * line = [[UIImageView alloc]init];
    
    line.backgroundColor = [UIColor colorWithHexString:@"00C5C5"];
    
    [self.view addSubview:line];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(topView.mas_bottom);
        
        make.right.equalTo(self.view).offset(-13);
        make.height.equalTo(@1.5);
        make.left.equalTo(self.view).offset(31.5);
        
    }];
    
    self.tempView = line;

    //分割线2
    UIImageView * line1 = [[UIImageView alloc]init];
    
    line1.backgroundColor = [UIColor colorWithHexString:@"00C5C5"];
    
    [self.view addSubview:line1];
    
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(tableView.mas_bottom);
        
        make.right.equalTo(self.view).offset(-13);
        make.height.equalTo(@1.5);
        make.left.equalTo(self.view).offset(31.5);
        
    }];

    

}

- (void)brnAction:(UIButton *)btn{
    
    NKCaseAddOrmodifyVC * addVC = [[NKCaseAddOrmodifyVC alloc]init];
    if (addVC.caseInforList == nil) {
         addVC.caseInforList = [[NSMutableArray alloc]init];
    }
   
    
    addVC.caseInforList = self.caseInforList;
    
    addVC.title = @"新加病例";
    
    [self.navigationController pushViewController:addVC animated:YES];
    
    
}

// 实现数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableViewP{
    
    return  1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _caseInforList.count;
}

- (NKCaseSaveCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NKCaseSaveCell * cell = [tableView dequeueReusableCellWithIdentifier:@"KAKA" forIndexPath:indexPath];
  
    cell.timeLable.text = _caseInforList[indexPath.row].saveDate;
    cell.illDisCreb.text = _caseInforList[indexPath.row].caseDiscreb;
    cell.illNameLable.text = _caseInforList[indexPath.row].caseName;

    if (_caseInforList[indexPath.row].illImage ) {
    
        cell.illImageView.image = _caseInforList[indexPath.row].illImage;
    
    
    
    }
    
    
    return cell;
    
}

//tableview代理方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    NKCaseAddOrmodifyVC * vc = [[NKCaseAddOrmodifyVC alloc]init];
    
    vc.title = @"修改病例";
    
    [self.navigationController pushViewController:vc animated:YES];
//
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
